package cn.fl.custpay.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.fl.custpay.condition.CmmIntelChdOrderQC;
import cn.fl.custpay.condition.CmmIntelOrderQC;
import cn.fl.custpay.service.ICmmIntelOrderService;
import cn.fl.custpay.vo.CmmIntelChdOrderVO;
import cn.fl.custpay.vo.CmmIntelOrderVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import fl.constant.facade.framework.constant.CmmCustPayConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;

import java.util.List;

/**
 * TODO 智能收款订单记录表 控制层
 * @package: cn.fl.custpay.controller
 * @className: CmmIntelOrderController
 * @author: shijibin@lionbridgecapital.cn
 * @createDate: 2019/3/19 15:39
 * @version: V1.0.0
 * @copyright: (c) 2019
 * @updateRemark: [序号][YYYY-MM-DD] [更改人姓名][变更描述]
 */
@Controller
@RequestMapping("/custpay/cmmIntelOrder")
public class CmmIntelOrderController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(CmmIntelOrderController.class);

	/**
	 * CmmIntelOrder 服务层BEAN
	 */
	@Autowired
	private ICmmIntelOrderService cmmIntelOrderService;

	/**
	 *  数据字典服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	/**
	 * TODO 到列表页面
	 * @methodName toListPage
	 * @author: shijibin@lionbridgecapital.cn
	 * @createDate: 2019/3/19 16:26
	 * @version: 1.0.0
	 * @param request   请求对象
	 * @param response  响应对象
	 * @return org.springframework.web.servlet.ModelAndView
	 * @updateRemark [序号][YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toListPage", "加载列表页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		//订单状态
		String orderStsCd = CmmCustPayConstant.CMM_INTEL_ORDER_STS_CD;
		//业务类型
		String busTypCd = CmmCustPayConstant.CMM_INTEL_BUS_TYP_CD;
		List<KeyValueVO> orderStsList = this.sysDictionaryApiService.getKvList(orderStsCd, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
		List<KeyValueVO> busTypCdList = this.sysDictionaryApiService.getKvList(busTypCd, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());

		ModelAndView view = new ModelAndView("custpay/cmmIntelOrder/cmmIntelOrderList");
		view.addObject("orderStsList", orderStsList);
		view.addObject("busTypCdList",busTypCdList);
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toListPage", "加载列表页面.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return view;
	}


	/**
	 * TODO 初始化列表
	 * @methodName initList
	 * @author: shijibin@lionbridgecapital.cn
	 * @createDate: 2019/3/19 16:30
	 * @version: 1.0.0
	 * @param request   请求对象
	 * @param response  响应对象
	 * @return java.lang.Object
	 * @updateRemark [序号][YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "initList", "加载列表页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		//客户名称
		String clientName = request.getParameter("clientName");
		//客户手机号
		String clientPhone = request.getParameter("clientPhone");
		//收款账号
		String payAccount = request.getParameter("payAccount");
		//订单状态
		String orderStsCd = request.getParameter("orderStsCd");
		//业务类型
		String busTypeCd = request.getParameter("busTypeCd");
		//创建时间(起)
		String crtTmBegin = request.getParameter("crtTmBegin");
		//创建时间(止)
		String crtTmEnd = request.getParameter("crtTmEnd");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		CmmIntelOrderQC qc = new CmmIntelOrderQC();
		if (StringUtils.isNotEmpty(clientName)) {
			qc.setClientName(clientName);
		}
		if(StringUtils.isNotEmpty(clientPhone)){
			qc.setClientPhone(clientPhone);
		}
		if(StringUtils.isNotEmpty(payAccount)){
			qc.setPayAccount(payAccount);
		}
		if(StringUtils.isNotEmpty(busTypeCd)){
			qc.setBusTypeCd(busTypeCd);
		}
		if(StringUtils.isNotEmpty(orderStsCd)){
			qc.setOrderStsCd(orderStsCd);
		}
		if (StringUtils.isNotEmpty(crtTmBegin)) {
			qc.setCrtTmBegin(DateUtils.formatStringToDate(crtTmBegin,DateUtils.YYYY_MM_DD));
		}
		if (StringUtils.isNotEmpty(crtTmEnd)) {
			qc.setCrtTmEnd(DateUtils.formatStringToDate(crtTmEnd, DateUtils.YYYY_MM_DD));
		}
		page.setCondition(qc);

		//TODO SOMETHING
		Pager pager = this.cmmIntelOrderService.searchListPage(page);
		List<CmmIntelOrderVO> list = (List<CmmIntelOrderVO>) pager.getResultList();
		ListPageVO<CmmIntelOrderVO> listPage = new ListPageVO<CmmIntelOrderVO>(list, pager.getTotalCount());
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "initList", "加载列表页面.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}

	/**
	 * TODO 新增
	 * @methodName add
	 * @author: shijibin@lionbridgecapital.cn
	 * @createDate: 2019/3/19 16:31
	 * @version: 1.0.0
	 * @param request   请求对象
	 * @param response  响应对象
	 * @return java.lang.Object
	 * @updateRemark [序号][YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "add", "新增对象.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		String name = request.getParameter("name");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "新增成功!");
		CmmIntelOrderVO entity = null;
		try {
			entity = new CmmIntelOrderVO();
			//TODO ADD PARAM
			Long id = this.cmmIntelOrderService.add(entity);
			result.setData(id);
			BaseLogUtils.info(logger, "add", "新增数据.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "add", "新增对象失败,name["+name+"].", CurrentThreadContext.getCurrentUserName(), ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.error(logger, "add", "新增对象失败,name["+name+"].", CurrentThreadContext.getCurrentUserName(), ex);
		}
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "add", "新增对象.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		
		return result;
	}

	/**
	 * TODO 修改
	 * @methodName modify
	 * @author: shijibin@lionbridgecapital.cn
	 * @createDate: 2019/3/19 16:31
	 * @version: 1.0.0
	 * @param request   请求对象
	 * @param response  响应对象
	 * @return java.lang.Object
	 * @updateRemark [序号][YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "modify", "修改对象.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "修改成功!");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("数据不存在!");
			BaseLogUtils.info(logger, "modify", "修改数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		CmmIntelOrderVO entity = null;
		try {
			entity = new CmmIntelOrderVO();
			entity.setId(Long.valueOf(id));
			//entity.setName(name);
			//TODO ADD PARAM
			boolean success = this.cmmIntelOrderService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("修改失败!");
				BaseLogUtils.info(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "modify", "修改成功.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modify", "修改对象失败,id["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.error(logger, "modify", "修改对象失败,id["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		}
		
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "modify", "修改对象.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());

		return result;
	}

	/**
	 * TODO 删除数据
	 * @methodName removeById
	 * @author: shijibin@lionbridgecapital.cn
	 * @createDate: 2019/3/19 16:32
	 * @version: 1.0.0
	 * @param request   请求对象
	 * @param response  响应对象
	 * @return cmm.mid.core.framework.page.DataResultVO
	 * @updateRemark [序号][YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@RequestMapping(value = "/removeById.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "remove", "移除对象.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "删除成功!");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("主键不能为空!");
			return result;
		}
		try {
			this.cmmIntelOrderService.removeById(Long.valueOf(id));
			BaseLogUtils.info(logger, "removeById", "删除数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "remove", "移除对象失败,主键["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.error(logger, "remove", "移除对象失败,主键["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		}
		
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "remove", "移除对象.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());

		return result;
	}

	/**
	 * TODO 得到一条记录
	 * @methodName getById
	 * @author: shijibin@lionbridgecapital.cn
	 * @createDate: 2019/3/19 16:32
	 * @version: 1.0.0
	 * @param request   请求对象
	 * @param response  响应对象
	 * @return java.lang.Object
	 * @updateRemark [序号][YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "getById", "根据主键获取对象.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "操作成功!");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("主键不能为空!");
			BaseLogUtils.error(logger, "getById", "获取数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		
		try {
			CmmIntelOrderVO vo = this.cmmIntelOrderService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("数据不存在!");
				BaseLogUtils.info(logger, "getById", "获取数据,数据不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			}
			result.setData(vo);
			BaseLogUtils.info(logger, "getById", "获取数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getById", "根据主键获取对象失败,主键["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.error(logger, "getById", "根据主键获取对象失败,主键["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		}
		
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "getById", "根据主键获取对象.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());

		return result;
	}

	/**
	 * TODO 根据父订单查询子订单列表
	 * @methodName initList
	 * @author: shijibin@lionbridgecapital.cn
	 * @createDate: 2019/3/19 16:30
	 * @version: 1.0.0
	 * @param request   请求对象
	 * @param response  响应对象
	 * @return java.lang.Object
	 * @updateRemark [序号][YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@RequestMapping(value = "/searchChdListByParentId.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object searchChdListByParentId(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "initList", "智能汇款根据父订单查询子订单列表.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());

		//父订单id
		String parOrderId = request.getParameter("parOrderId");

		if (StringUtils.isEmpty(parOrderId)) {
			BaseLogUtils.info(logger, "searchChdListByParentId", "父订单id不能为空", CurrentThreadContext.getCurrentUserName());
			return null;
		}

		//TODO SOMETHING
		List<CmmIntelChdOrderVO> list = this.cmmIntelOrderService.searchChdListByParentId(Long.valueOf(parOrderId));

		ListPageVO<CmmIntelChdOrderVO> listPage = null;

		if(CollectionUtils.isNotEmpty(list)){
            listPage = new ListPageVO<>(list, list.size());
        }

		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "searchChdListByParentId", "智能汇款根据父订单查询子订单列表.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());

		return listPage;
	}
}

